home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / full / jbuild / setup / JBuilder / jsamples.z / buttons.jar / sunw / demo / buttons / OurButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-04  |  5.9 KB  |  244 lines

  1. package sunw.demo.buttons;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.PropertyChangeSupport;
  17. import java.io.Serializable;
  18. import java.util.Vector;
  19.  
  20. public class OurButton extends Component implements Serializable, MouseListener, MouseMotionListener {
  21.    private boolean debug;
  22.    private PropertyChangeSupport changes;
  23.    private Vector pushListeners;
  24.    private String label;
  25.    private boolean down;
  26.    private boolean sized;
  27.    static final int TEXT_XPAD = 12;
  28.    static final int TEXT_YPAD = 8;
  29.  
  30.    public OurButton() {
  31.       this("press");
  32.    }
  33.  
  34.    public OurButton(String var1) {
  35.       this.changes = new PropertyChangeSupport(this);
  36.       this.pushListeners = new Vector();
  37.       this.label = var1;
  38.       this.setFont(new Font("Dialog", 0, 12));
  39.       this.setBackground(Color.lightGray);
  40.       ((Component)this).addMouseListener(this);
  41.       ((Component)this).addMouseMotionListener(this);
  42.    }
  43.  
  44.    public synchronized void paint(Graphics var1) {
  45.       int var2 = ((Component)this).getSize().width;
  46.       int var3 = ((Component)this).getSize().height;
  47.       var1.setColor(((Component)this).getBackground());
  48.       var1.fill3DRect(0, 0, var2 - 1, var3 - 1, !this.down);
  49.       var1.setColor(((Component)this).getForeground());
  50.       var1.setFont(((Component)this).getFont());
  51.       var1.drawRect(2, 2, var2 - 4, var3 - 4);
  52.       FontMetrics var4 = var1.getFontMetrics();
  53.       var1.drawString(this.label, (var2 - var4.stringWidth(this.label)) / 2, (var3 + var4.getMaxAscent() - var4.getMaxDescent()) / 2);
  54.    }
  55.  
  56.    public void mouseClicked(MouseEvent var1) {
  57.    }
  58.  
  59.    public void mousePressed(MouseEvent var1) {
  60.       if (((Component)this).isEnabled()) {
  61.          this.down = true;
  62.          ((Component)this).repaint();
  63.       }
  64.    }
  65.  
  66.    public void mouseReleased(MouseEvent var1) {
  67.       if (((Component)this).isEnabled()) {
  68.          if (this.down) {
  69.             this.fireAction();
  70.             this.down = false;
  71.             ((Component)this).repaint();
  72.          }
  73.  
  74.       }
  75.    }
  76.  
  77.    public void mouseEntered(MouseEvent var1) {
  78.    }
  79.  
  80.    public void mouseExited(MouseEvent var1) {
  81.    }
  82.  
  83.    public void mouseDragged(MouseEvent var1) {
  84.       if (((Component)this).isEnabled()) {
  85.          int var2 = var1.getX();
  86.          int var3 = var1.getY();
  87.          int var4 = ((Component)this).getSize().width;
  88.          int var5 = ((Component)this).getSize().height;
  89.          if (var2 >= 0 && var2 <= var4 && var3 >= 0 && var3 <= var5) {
  90.             if (!this.down) {
  91.                this.down = true;
  92.                ((Component)this).repaint();
  93.             }
  94.          } else if (this.down) {
  95.             this.down = false;
  96.             ((Component)this).repaint();
  97.             return;
  98.          }
  99.  
  100.       }
  101.    }
  102.  
  103.    public void mouseMoved(MouseEvent var1) {
  104.    }
  105.  
  106.    public synchronized void addActionListener(ActionListener var1) {
  107.       this.pushListeners.addElement(var1);
  108.    }
  109.  
  110.    public synchronized void removeActionListener(ActionListener var1) {
  111.       this.pushListeners.removeElement(var1);
  112.    }
  113.  
  114.    public void addPropertyChangeListener(PropertyChangeListener var1) {
  115.       this.changes.addPropertyChangeListener(var1);
  116.    }
  117.  
  118.    public void removePropertyChangeListener(PropertyChangeListener var1) {
  119.       this.changes.removePropertyChangeListener(var1);
  120.    }
  121.  
  122.    public void fireAction() {
  123.       if (this.debug) {
  124.          System.err.println("Button " + this.getLabel() + " pressed.");
  125.       }
  126.  
  127.       synchronized(this){}
  128.  
  129.       Vector var1;
  130.       try {
  131.          var1 = (Vector)this.pushListeners.clone();
  132.       } catch (Throwable var6) {
  133.          throw var6;
  134.       }
  135.  
  136.       ActionEvent var2 = new ActionEvent(this, 0, (String)null);
  137.  
  138.       for(int var3 = 0; var3 < var1.size(); ++var3) {
  139.          ActionListener var4 = (ActionListener)var1.elementAt(var3);
  140.          var4.actionPerformed(var2);
  141.       }
  142.  
  143.    }
  144.  
  145.    public void setDebug(boolean var1) {
  146.       boolean var2 = this.debug;
  147.       this.debug = var1;
  148.       this.changes.firePropertyChange("debug", new Boolean(var2), new Boolean(var1));
  149.    }
  150.  
  151.    public boolean getDebug() {
  152.       return this.debug;
  153.    }
  154.  
  155.    public void setLargeFont(boolean var1) {
  156.       if (this.isLargeFont() != var1) {
  157.          byte var2 = 12;
  158.          if (var1) {
  159.             var2 = 18;
  160.          }
  161.  
  162.          Font var3 = ((Component)this).getFont();
  163.          this.setFont(new Font(var3.getName(), var3.getStyle(), var2));
  164.          this.changes.firePropertyChange("largeFont", new Boolean(!var1), new Boolean(var1));
  165.       }
  166.    }
  167.  
  168.    public boolean isLargeFont() {
  169.       return ((Component)this).getFont().getSize() >= 18;
  170.    }
  171.  
  172.    public void setFontSize(int var1) {
  173.       Font var2 = ((Component)this).getFont();
  174.       this.setFont(new Font(var2.getName(), var2.getStyle(), var1));
  175.       this.changes.firePropertyChange("fontSize", new Integer(var2.getSize()), new Integer(var1));
  176.    }
  177.  
  178.    public int getFontSize() {
  179.       return ((Component)this).getFont().getSize();
  180.    }
  181.  
  182.    public void setFont(Font var1) {
  183.       Font var2 = ((Component)this).getFont();
  184.       super.setFont(var1);
  185.       this.sizeToFit();
  186.       this.changes.firePropertyChange("font", var2, var1);
  187.    }
  188.  
  189.    public void setLabel(String var1) {
  190.       String var2 = this.label;
  191.       this.label = var1;
  192.       this.sizeToFit();
  193.       this.changes.firePropertyChange("label", var2, var1);
  194.    }
  195.  
  196.    public String getLabel() {
  197.       return this.label;
  198.    }
  199.  
  200.    public Dimension getPreferredSize() {
  201.       FontMetrics var1 = ((Component)this).getFontMetrics(((Component)this).getFont());
  202.       return new Dimension(var1.stringWidth(this.label) + 12, var1.getMaxAscent() + var1.getMaxDescent() + 8);
  203.    }
  204.  
  205.    /** @deprecated */
  206.    public Dimension preferredSize() {
  207.       return this.getPreferredSize();
  208.    }
  209.  
  210.    public Dimension getMinimumSize() {
  211.       return this.getPreferredSize();
  212.    }
  213.  
  214.    /** @deprecated */
  215.    public Dimension minimumSize() {
  216.       return this.getMinimumSize();
  217.    }
  218.  
  219.    private void sizeToFit() {
  220.       Dimension var1 = this.getPreferredSize();
  221.       ((Component)this).setSize(var1.width, var1.height);
  222.       Container var2 = ((Component)this).getParent();
  223.       if (var2 != null) {
  224.          ((Component)var2).invalidate();
  225.          ((Component)var2).doLayout();
  226.       }
  227.  
  228.    }
  229.  
  230.    public void setForeground(Color var1) {
  231.       Color var2 = ((Component)this).getForeground();
  232.       super.setForeground(var1);
  233.       this.changes.firePropertyChange("foreground", var2, var1);
  234.       ((Component)this).repaint();
  235.    }
  236.  
  237.    public void setBackground(Color var1) {
  238.       Color var2 = ((Component)this).getBackground();
  239.       super.setBackground(var1);
  240.       this.changes.firePropertyChange("background", var2, var1);
  241.       ((Component)this).repaint();
  242.    }
  243. }
  244.